using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;
using NTTHAO.Imaging;

public partial class admin_webparts_Plug_ins_Files_UploadFile : System.Web.UI.UserControl
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";
    public static string[] _EXCLUDED_EX = new string[] { ".php", ".asp", ".aspx", ".java", ".py", ".cfm", ".cfml", ".vb", ".cs" };

    public string _Message = string.Empty;
    public string _CurrentPath = "/";

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave1.Text = LanguageUtility.GetLanguageText("UPLOAD", Session);

        try
        {
            string action = Request.QueryString["action"];

            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }

            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));
            if (!Directory.Exists(currentServerPath))
            {
                Response.Redirect("?type=page&name=files");
                return;
            }

            //if (!IsPostBack)
            //{
            //    chkLayerMask.Checked = Constant.ADD_LAYER_MASK;
            //}
        }
        catch (Exception ex)
        {
            _Message = ex.Message;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));
            string msg = "";

            msg += this.SaveFile(fuFile1, currentServerPath);
            msg += this.SaveFile(fuFile2, currentServerPath);
            msg += this.SaveFile(fuFile3, currentServerPath);
            msg += this.SaveFile(fuFile4, currentServerPath);
            msg += this.SaveFile(fuFile5, currentServerPath);
            msg += this.SaveFile(fuFile6, currentServerPath);
            msg += this.SaveFile(fuFile7, currentServerPath);
            msg += this.SaveFile(fuFile8, currentServerPath);
            msg += this.SaveFile(fuFile9, currentServerPath);
            msg += this.SaveFile(fuFile10, currentServerPath);

            if (msg != "")
            {
                msg = "<ul>" + msg + "</ul>";
                this.SetCookie(FILES_MESSAGE_TYPE, "-1");
                this.SetCookie(FILES_MESSAGE, msg);
            }
            else
            {
                this.SetCookie(FILES_MESSAGE_TYPE, "0");
                this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("UPLOADED_FILES_SAVED", Session));
            }

            Response.Redirect("?type=page&name=files&file=" + Server.UrlEncode(this._CurrentPath), false);
            return;
        }
        catch (Exception ex)
        {
            _Message = ex.Message;
        }
    }

    protected string SaveFile(FileUpload fulFile, string serverPath)
    {
        string msg = "";
        if (fulFile.HasFile)
        {
            try
            {
                string filePath = Path.Combine(serverPath, fulFile.FileName);

                string fileEx = Path.GetExtension(filePath);
                if (fileEx == null || fileEx.Trim() == string.Empty)
                {
                    return "";
                }
                fileEx = fileEx.Trim();
                foreach (string ex in _EXCLUDED_EX)
                {
                    if (fileEx.StartsWith(ex, StringComparison.OrdinalIgnoreCase))
                    {
                        return "<li>" + fulFile.FileName + ": File extension not allowed for upload." + "</li>";
                    }
                }

                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                fulFile.PostedFile.SaveAs(filePath);

                if (chkLayerMask.Checked && ".jpg".Equals(fileEx, StringComparison.OrdinalIgnoreCase))
                {
                    this.AddLayerMask(filePath);
                }
            }
            catch (Exception ex)
            {
                msg = "<li>" + ex.Message + "</li>";
            }
        }
        return msg;
    }

    protected void AddLayerMask(string serverFileName)
    {
        //layer mask
        if (Constant.ADD_LAYER_MASK)
        {
            float left = 0, top = 0, width = 0, height = 0, opacity = 1;
            float.TryParse(ConfigValues.GetConfigValue("MASK_TOP_PERCENT").Trim(), out top);
            float.TryParse(ConfigValues.GetConfigValue("MASK_LEFT_PERCENT").Trim(), out left);
            float.TryParse(ConfigValues.GetConfigValue("MASK_WIDTH_PERCENT").Trim(), out width);
            float.TryParse(ConfigValues.GetConfigValue("MASK_HEIGHT_PERCENT").Trim(), out height);
            float.TryParse(ConfigValues.GetConfigValue("MASK_OPACITY").Trim(), out opacity);

            ImageUtilities.DrawLayerMask(serverFileName, Server.MapPath("../" + Constant.MASK_IMAGE_FILE), top, left, width, height, ResizeOptions.MaximumSizeFix, opacity, ImageUtilities.HIGHT_PROGRESSIVE);
        }
        // end
    }

    public void SetCookie(string name, string value)
    {
        //CookieUtility.SetCookie(name, value, DateTime.Now.AddDays(1), Request, Response);
        Session[name] = value;
    }
}
